const Point = require("./Point");

/**
 * 向量类
 */
class Vector {
    constructor(startPoint, endPoint) {
        this.p1 = startPoint
        this.p2 = endPoint
    }

    /**
     * 根据起始点距离，每10米左右补充一个中间点
     * 目的是为了坐标点绑路的时候，更精确
     * 如果不足10米，则返回空数组
     * @returns [Point] 补全的中间点数组
     */
    resample() {
        let arr = []
        let distance = this.p1.distance(this.p2)
        let n = Math.round(distance / 10) - 1
        if (n <= 0) {
            return arr
        }
        for (let i = 1; i <= n; i++) {
            let lng = this.p1.lng + (this.p2.lng - this.p1.lng) / n * i
            let lat = this.p1.lat + (this.p2.lat - this.p1.lat) / n * i
            let p = new Point(lng, lat, 0)
            p.from = this.p1
            p.to = this.p2
            arr.push(p)
        }
        return arr
    }
}

module.exports = Vector;